package it.unibo.refolding.pico.mfp;

import java.nio.ByteBuffer;
import java.util.Objects;

class BinHeader2 extends BinHeader {

  /**
   * @param wfmSize
   *          The size of the WaveHeader2 data structure plus the wave data plus
   *          16 bytes of padding.
   * @param noteSize
   * @param checksum
   */
  public BinHeader2(int wfmSize, int noteSize, short checksum) {
    super(checksum, wfmSize, noteSize);
  }

  static Checksummed<BinHeader2> read(ByteBuffer bb) {
    int startPos = bb.position();
    int unsignedChecksum = 0;
    int wfmSize = bb.getInt();
    unsignedChecksum = IgorUtil.addInt(wfmSize, unsignedChecksum);
    int noteSize = bb.getInt();
    unsignedChecksum = IgorUtil.addInt(noteSize, unsignedChecksum);
    /*
     * Skip: long pictSize; // Reserved. Write zero. Ignore on read.
     */
    unsignedChecksum = IgorUtil.skip(4, unsignedChecksum, bb);
    short checksum = bb.getShort();
    unsignedChecksum = IgorUtil.addShort(checksum, unsignedChecksum);
    assert startPos + byteCount() == bb.position();
    return new Checksummed<>(new BinHeader2(wfmSize, noteSize, checksum), (short) unsignedChecksum);
  }

  static int byteCount() {
    return BinHeader.byteCount() + 4;
  }

  @Override
  public int hashCode() {
    return Objects.hash(wfmSize, noteSize, checksum);
  }
}
